
local key_limit1 = {}
local key_limit2 = {}
local key_limit3 = {}
local joint_limit = {}
local mt = {}
ac.score = mt

local function split_int(str)
    local data = split_string(str,'-')
    for i,num in ipairs(data) do
        data[i] = tonumber(num)
    end
    return data
end

local function conver_time(date)
    local time = 0
    local h = date.hour
    local m = date.min
    local s = date.sec
    return h*3600 + m*60 + s
end
local function split_date(str)
    local data = split_string(str,'-')
    for i,str1 in ipairs(data) do
        local h,m,s = str1:match('(%d%d):(%d%d):(%d%d)')
        if h and m and s then
            data[i] = h*3600 + m*60 + s
        end
    end
    return data
end


--添加一层每周上传限制(日期/时间段相关)
--- @params key 积分项
--- @params weeks 有效上传 '1-2-3' / '*'
--- @params times 有效时间段 可缺省
function mt.create_limit1(key,weeks,times)
    local data = {}
    if not key_limit1[key] then
        key_limit1[key] = {}
    end
    table.insert(key_limit1[key],data)

    --有效上传
    if weeks~='*' then
        data.weeks = {}
        for _,week in ipairs(split_int(weeks)) do
            data.weeks[week] = true
        end
    end

    --有效时间段
    local times = times or {'00:00:00-24:00:00'}
    if type(times)=='table' then
        for i,date in ipairs(times) do
            times[i] = split_date(date)
        end
        data.times = times
    else
        LogInfo('积分限制错误时间格式不对[%s]',key)
    end

end

--添加一层积分上限限制(等级/游戏时间相关)
--- @params key 积分项
--- @params min_level 最小等级
--- @params max_level 最大等级
--- @params everyDay 每天上限
--- @params uploadtime 有效上传时间 能缺省  0-600-1200
--- @params uploaddata 有效上传值  能缺省 1-2 
function mt.create_limit2(key,min_level,max_level,everyDay,uploadtime,uploaddata)
    local data = {}
    local uploadtime = uploadtime or '0-36000'
    local uploaddata = uploaddata or tostring(everyDay)
    if type(uploadtime)=='string' then
        uploadtime = split_int(uploadtime)
    end
    if type(uploaddata)=='string' then
        uploadtime = split_int(uploaddata)
    end

    if not key_limit2[key] then
        key_limit2[key] = {}
    end
    table.insert(key_limit2[key],data)
    
    --有效上传等级
    data.min_level = min_level
    data.max_level = max_level

    --每天上限
    data.everyDay = everyDay
    --有效上传时间
    data.uploadtime = uploadtime
    data.uploaddata = uploaddata
    return data
end


--添加一层联合积分限制
--- @params name 唯一命名不能和存档名一样
--- @params everyDay 每天总和
--- @params everyGame 每局总和
--- @params ... 存档字段
function mt.create_limit3(name,everyDay,everyGame,...)
    local data = {}
    local keys = {...}

    if joint_limit[name] then
        LogInfo('积分限制命名重复3[%s]',name)
        return
    end
    joint_limit[name] = data
    data.name = name
    data.everyDay = everyDay
    data.everyGame = everyGame
    data.keys = keys

    for _,key in ipairs(keys) do
        if not key_limit3[key] then
            key_limit3[key] = {}
        end
        table.insert(key_limit3[key],data)
    end

end



--检查限制1 存档能否上传
function mt.check_week(key)
    local limit = key_limit1[key]
    local week = ac.game.week
    local now_time = os.time()
    if not limit then
        return true
    end
    
    for _,data in ipairs(limit) do
        --星期检查
        if data.weeks=='*' or data.weeks[week] then
            --时间点检查
            for _,times in ipairs(data.times) do
                local min = times[1]
                local max = times[2]
                if now_time>=min and now_time<=max then
                    return true
                end
            end
        else
            
        end
    end
    return false
end


--检查存档注册是否符合规则
function mt.check_register()
    --联合存档名字是否和正常存档名字冲突
    for key,data in pairs(joint_limit) do
        if key_limit1[key] or key_limit2[key] or key_limit3[key] then
            LogInfo('联合存档命名重复[%s]',key)
        end
    end

    --写法符合规则
    for key,list in pairs(key_limit1) do
        for _,data in ipairs(list) do
            if type(data.weeks)~='table' and data.weeks~='*' and #data.weeks==0 then
                LogInfo('限制1~日期不合法[%s]%s',key,data.weeks)
            end
            for _,data in ipairs(data.times) do
                if #data~=2 then
                    LogInfo('限制1~时间不合法[%s]%s',key,#data)
                end
            end
        end
    end
    for key,list in pairs(key_limit2) do
        for _,data in ipairs(list) do
            if data.everyDay==nil then
                LogInfo('积分限制无每天数据[%s]',key)
            end
            if #data.uploadtime~=#data.uploaddata+1 then
                LogInfo('积分限制错误2[%s]',key)
            end
        end
    end
end

--读取存档剩余上限
function ac.player.__index:check_key_max(key)
    local game_data = self._game_data
    local days_data = self._days_data
    local level = self.map_level
    local run_time = ac.game:get_time()
    
    --星期时间段检查
    if not mt.check_week(key) then
        return 0
    end

    local remain1 = {}
    local remain2 = {}

    --获取当前时间段允许上限
    local limit = key_limit2[key]
    if limit then
        local has1 = 0
        local has2 = 0
        for _,data in ipairs(limit) do
            if level>=data.min_level and level<=data.max_level then
                for i=1,#data.uploadtime-1 do
                    local min = data.uploadtime[i]
                    local max = data.uploadtime[i+1]
                    local num = data.uploaddata[i]
                    if run_time>min and run_time<=max then
                        has1 = data.everyDay - (days_data[key] or 0 )
                        has2 = num - (game_data[key] or 0)
                        break
                    end
                end
                break
            end
        end
        table.insert(remain1,has1)
        table.insert(remain2,has2)
    end

    --联合存档剩余上限
    local limit = key_limit3[key]
    if limit then
        for _,data in ipairs(limit) do
            local name = data.name
            if data.everyDay then
                local max = data.everyDay - (days_data[name] or 0)
                table.insert(remain1,max)
            end
            if data.everyGame then
                local max = data.everyGame - (game_data[name] or 0)
                table.insert(remain2,max)
            end
        end
    end
    if #remain1>0 and #remain2>0 then
        local min1 = math.min(table.unpack(remain1))
        local min2 = math.min(table.unpack(remain2))
        return math.min(min1,min2),min1,min2
    end
    
    return -1
end


--检查所有存档剩余每天上限
function ac.player.__index:check_all_max()
    local days_data = self._days_data
    local level = self.map_level
    local info = {}

    for key,limit in pairs(key_limit2) do
        local day = days_data[key] or 0
        for _,data in ipairs(limit) do
            if level>=data.min_level and level<=data.max_level then
                info[key] = {day,data.everyDay}
                break
            end
        end
    end
    
    for a,data in pairs(joint_limit) do
        local key = data.name
        local day = days_data[key] or 0
        info[key] = {day,data.everyDay}
    end

    return info
end

--添加数据检测
function ac.player.__index:add(key,num)
    local game_data = self._game_data
    local days_data = self._days_data
    local max = self:check_key_max(key)
    if max>0 then
        local change = math.min(num,max)
        game_data[key] = (game_data[key] or 0) + change
        days_data[key] = (days_data[key] or 0) + change
        self:set(key,self:get(key,0) + change)
    elseif max==-1 then
        self:set(key,self:get(key,0) + num)
    end
end

--获取每日使用数据
function ac.player.__index:daily_get(key,default)
    local days_data = self._days_data
    return days_data[key] or default
end

--设置每日使用数据
function ac.player.__index:daily_set(key,num)
    local days_data = self._days_data
    days_data[key] = num
end

--添加每日使用数据
function ac.player.__index:daily_add(key,num)
    local days_data = self._days_data
    days_data[key] = (days_data[key] or 0) + num
end


--初始化
ac.game:event '玩家-初始化数据'(function(_,player)
    local data = player:get('每天暂存') or {}
    player:set('每天暂存',data)
    player._game_data = {}
    player._days_data = data
end)

--新的一天
ac.game:event '玩家-新的一天'(function(_,player)
    local data = {}
    player:set('每天暂存',data)
    player._days_data = data
end)

return mt